home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Documentation / Engineering Notes / Miscellaneous / File & Resource I⁄O < prev    next >
Encoding:
Text File  |  1996-08-16  |  5.2 KB  |  95 lines  |  [TEXT/ttxt]

  1. OpenDoc
  2. Development
  3. Framework
  4.                                                                                                                                                                                      
  5. File & Resource I/O
  6. ODF Release 1                                                                                                                                                             
  7.  
  8.  
  9. Table of Contents
  10. -------------------
  11. • How to Write to a File
  12. • How to Read From a File
  13. • How to Write to a Resource
  14. • How to Read From a Resource
  15. • Why Use Buffered I/O?
  16. • How to Do Buffered I/O
  17.  
  18.  
  19. This document describes how to use streams and sinks to perform simple I/O operations.  Streams format the data between client and sink.  Sinks are low–level, transferring uninterpreted bytes to/from a file, OpenDoc storage unit, etc.  The client of a stream writes, and having writ moves on, neither knowing nor caring if the data went to a file, an OpenDoc storage unit or an in–memory buffer.  
  20.  
  21. In ODF, sinks are SOM objects and streams are C++ objects.  Most clients of sinks never see the naked sink; they use smart pointer classes whose constructors instantiate the sink and whose destructors free it.  The sink is passed to a stream constructor and the client reads or writes data to/from the stream.
  22.  
  23. The ODF stream and sink classes are a powerful I/O abstraction.  However, the power of the stream/sink paradigm has one drawback: it is not immediately obvious how a client can open a file and read it.  The examples below are mostly derived from code shipped with ODF.
  24.  
  25.  
  26. How to Write to a File
  27.  
  28. To write to a file, you need three ingredients: an FW_PFile, an FW_PFileSink, and an FW_CWritableStream.  The FW_PFile can be created from an FW_PFileSpecification or from an existing FW_OFileRep.  The FW_PFileSink is constructed using the FW_PFile.  The FW_CWritableStream is constructed from the FW_PFileSink.  The example below is from internal test code.
  29.  
  30.         FW_PFileSpecification fileSpec(ev, fileName);
  31.         FW_CAccessPermission permission(FW_kWrite, FW_kDenyWrite);
  32.         FW_PFile theFile(ev, fileSpec, permission);
  33.         FW_PFileSink theSink(ev, theFile);
  34.         FW_CWritableStream aWStream(theSink);
  35.  
  36.         aWStream << "hello, world";
  37.  
  38.  
  39. How to Read From a File
  40.  
  41. Reading from a file is similar to writing.  An FW_PFile and FW_PFileSink are still required.  However, an FW_CReadableStream is used instead of the FW_CWritableStream.  The example below was extracted from ODFBitmap (CBitmapContent::InternalizePICTFile in Content.cpp).
  42.  
  43.         FW_PFileSpecification fileSpec(ev, fileName);
  44.         FW_CAccessPermission permission(FW_kRead, FW_kDenyWrite);
  45.         FW_PFile theFile(ev, fileSpec, permission);
  46.         FW_PFileSink theSink(ev, theFile);
  47.         FW_CReadableStream aRStream(theSink);
  48.         FW_CString stringFromFile;
  49.  
  50.         aRStream >> stringFromFile;
  51.  
  52.  
  53. How to Write to a Resource
  54.  
  55. You can't.  ODF does not currently have an interface for writing resources.  Resources are created using ODFRC (or other resouce builder) when the part is built and are then only read by the part.
  56.  
  57.  
  58. How to Read From a Resource
  59.  
  60. Resources come from FW_PResourceFiles.  FW_PResourceFiles are constructed from FW_PFileSpecifications.  The data within the resource can be read using an FW_CReadableStream or by direct examination of memory.  The  example below is from ODFButton (CScriptAction::InternalizeScriptFile in Actions.cpp).
  61.  
  62.         FW_PFileSpecification fileSpec(ev, fileName);
  63.         FW_PResourceFile resourceFile(ev, fileSpec);
  64.         FW_PResource aResource(ev, resourceFile, 128, 'scpt');
  65.  
  66. Having a resource, the data can be read into a handle with the following call:
  67.  
  68.         FW_CAcquireResourceData aResource(ev, resource);
  69.  
  70. If the data is an object (as created by ODFRC), the resource can be converted into a sink and passed to a stream as shown below.  (The snippet is from PRBitmap.cpp)
  71.  
  72.         FW_PResourceSink sink(ev, aResource);
  73.         FW_CReadableStream stream(sink);
  74.  
  75.  
  76. Why Use Buffered I/O?
  77.  
  78. For possible performance improvement.  When a stream reads and writes to a sink, it calls the sink's read and write methods.  Reading and writing to some sinks (such as an ODStorageUnitSink) is expensive because of the marshalling of arguments.  By buffering a sink, these expensive calls are made only when necessary (when the buffer is full or when a seek is done). 
  79.  
  80.  
  81. How to Do Buffered I/O?
  82.  
  83. ODF has a class called FW_PBufferedSink.  This sink wraps a buffer around an existing sink.  The constructor for FW_PBufferedSink takes the ever–present ev, a random access sink (resource and file sinks are random access sinks) and a buffer size.  When a stream is constructed from the buffered sink, all data transfers  are buffered.  As an example, we'll use the file writing example above and wrap a buffer around the file.
  84.  
  85.         FW_OFileRep *theFileRep = openFile();
  86.         FW_PFile theFile(ev, theFileRep);
  87.         FW_PFileSink theSink(ev, theFile);
  88.         FW_PBufferedSink theBufferedSink(ev, theSink, 1024);
  89.         FW_CWritableStream aWStream(theBufferedSink);
  90.  
  91.         aWStream << "hello, world";
  92.  
  93.  
  94. © 1993 - 1996 Apple Computer, Inc. All rights reserved.
  95. Apple, the Apple Logo, Macintosh, and OpenDoc are trademarks of Apple Computer, Inc., registered in the United States and other countries.